<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

$query = "SELECT admin FROM users WHERE user_id = " . $_SESSION[ 'user_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if( isset( $row[ 'admin' ] ) && $row[ 'admin' ] == 1 ) {
?>
<html>
<head>
<title>Ages of Strife: Admin user list</title>
<link type="text/css" rel="stylesheet" href="../global/agesofstrife.css" />
</head>
<body>
<?php
print "<div id=\"root_container\">\r\n";
include( "../global/ads.php" );
?>
<div id="container"><?php
	if( isset( $_GET[ 'type' ] ) ) {
		if( $_GET[ 'type' ] == 'public' ) {
			$type = 0;
			$title = " (public)";
		}
		else if( $_GET[ 'type' ] == 'faction' ) {
			$type = 1;
			$title = " (faction)";
		}
	}
	if( !isset( $type ) ) {
		$type = 2;
		$title = " (private)";
	}
	
    include_once( "../global/old_database_connection.php" );
	$query = "SELECT turn, turns_inactive FROM game";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );

    if( $type == 0 ) {
    	    $query = "SELECT name, ban_turn, banned, active_turn, accept_ruler, gold_want, iron_want, coal_want, " .
	    		"gold_offer, iron_offer, coal_offer, trade.ruler_id, complete_turn FROM trade " .
	    		"JOIN ruler ON ruler.ruler_id = accept_ruler " .
	    		"JOIN users ON ruler.user_id = users.user_id " .
	    		"WHERE trade.ruler_id != -1 AND accept_ruler != -1 AND private_id = -1 AND faction_id = -1 " .
	    		"ORDER BY accept_ruler, complete_turn";
    }
    else if( $type == 1 ) {
    	    $query = "SELECT name, ban_turn, banned, active_turn, accept_ruler, gold_want, iron_want, coal_want, " .
	    		"gold_offer, iron_offer, coal_offer, trade.ruler_id, complete_turn FROM trade " .
	    		"JOIN ruler ON ruler.ruler_id = trade.accept_ruler " .
	    		"JOIN users ON ruler.user_id = users.user_id " .
	    		"WHERE trade.ruler_id != -1 AND accept_ruler != -1 AND faction_id != -1 " .
	    		"ORDER BY accept_ruler, complete_turn";
    }
    else {
	    $query = "SELECT name, ban_turn, banned, active_turn, accept_ruler, gold_want, iron_want, coal_want, " .
	    		"gold_offer, iron_offer, coal_offer, trade.ruler_id, complete_turn FROM trade " .
	    		"JOIN ruler ON ruler.ruler_id = private_id " .
	    		"JOIN users ON ruler.user_id = users.user_id " .
	    		"WHERE trade.ruler_id != -1 AND accept_ruler != -1 " .
	    		"ORDER BY accept_ruler, complete_turn";
    }
    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
    $num = mysql_numrows( $result );
    print "<div id=\"main-content\">";
    print( "<a href=\"admin.php\">Back to the admin screen</a><br />\r\n" );
    print( "Current game turn: " . number_format( $row[ 'turn' ] ) . "<br /><br />" );
    if( isset( $_SESSION[ 'ban_ruler_status' ] ) ) {
    	print( $_SESSION[ 'ban_ruler_status' ] . "<br />\r\n" );
    	unset( $_SESSION[ 'ban_ruler_status' ] );
    }    
    print( "\r\n" );
    print "<table><th colspan=\"11\">Trade list" . $title . ": " . number_format( $num ) . ":</th>\r\n";
    print "<tr><td>Gold offer</td><td>Iron offer</td><td>Coal offer</td><td>Gold request</td><td>Iron request</td><td>Coal request</td>" .
    		"<td>Offered</td><td>Accepted</td><td>Finsihed</td><td>Ban for ? turns</td><td>Submit ban</td></tr>\r\n";
    $i = 0;
    $even = " class=\"even\"";
    $last_accept = "";
    while ( $i < $num ) {
    	if( $last_accept != mysql_result( $result, $i, "accept_ruler" ) ) {
    		if( $even == "" ) {
    			$even = " class=\"even\"";
    		}
    		else {
    			$even = "";
    		}
    		$last_accept = mysql_result( $result, $i, "accept_ruler" );
    	}
        print( "<tr" . $even . ">" );
        print( "<td align=\"right\"><strong id=\"gold\">" . number_format( mysql_result( $result, $i, "gold_offer" ) ) . "</strong></td>" );
        print( "<td align=\"right\"><strong id=\"iron\">" . number_format( mysql_result( $result, $i, "iron_offer" ) ) . "</strong></td>" );
        print( "<td align=\"right\"><strong id=\"coal\">" . number_format( mysql_result( $result, $i, "coal_offer" ) ) . "</strong></td>" );
        print( "<td align=\"right\"><strong id=\"gold\">" . number_format( mysql_result( $result, $i, "gold_want" ) ) . "</strong></td>" );
        print( "<td align=\"right\"><strong id=\"iron\">" . number_format( mysql_result( $result, $i, "iron_want" ) ) . "</strong></td>" );
        print( "<td align=\"right\"><strong id=\"coal\">" . number_format( mysql_result( $result, $i, "coal_want" ) ) . "</strong></td>" );
        
       	$query2 = "SELECT name FROM ruler WHERE ruler_id = " . mysql_result( $result, $i, "ruler_id" );
		$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
        print( "<td>" . $row2[ 'name' ] . "</td>" );
        
        print( "<td>" . mysql_result( $result, $i, "name" ) . "</td>" .
        		"<td>" . number_format( mysql_result( $result, $i, "complete_turn" ) ) . "</td>" );
        if( mysql_result( $result, $i, "banned" ) == 1 ) {
        	print( "<td>Permanently banned</td>" );
	        print( "<td><form method=\"post\" action=\"ban_ruler.php\">" .
	        		"<input type=\"hidden\" name=\"name\" value=\"" . mysql_result( $result, $i, "name" ) . "\" />" .
	        		"<input type=\"hidden\" name=\"unban\" value=\"unban\" /><input type=\"submit\" value=\"unban\" /></form></td></tr>\r\n" );
        }
        else if( mysql_result( $result, $i, "ban_turn" ) > $row[ 'turn' ] ) {
        	print( "<td>Banned until " . number_format( mysql_result( $result, $i, "ban_turn" ) ) . "</td>" );
	        print( "<td><form method=\"post\" action=\"ban_ruler.php\">" .
	        		"<input type=\"hidden\" name=\"name\" value=\"" . mysql_result( $result, $i, "name" ) . "\" />" .
	        		"<input type=\"hidden\" name=\"unban\" value=\"unban\" /><input type=\"submit\" value=\"unban\" /></form></td></tr>\r\n" );
        }
        else {
	        print( "<td><form method=\"post\" action=\"ban_ruler.php\">" .
	        		"<input type=\"hidden\" name=\"name\" value=\"" . mysql_result( $result, $i, "name" ) . "\" /><input type=\"text\" value=\"48\" size=\"4\" name=\"turns\" />" .
	        		"</td><td><input type=\"submit\" value=\"submit\" /></form></td></tr>\r\n" );
        }
        $i ++;
    }
    ?>	</table>
    </form>
</div>
</div>
</body>
</html>
	<?php
}
else {
	$_SESSION[ 'admin' ] = 0;
	header( "Location: ../overview/overview.php" );
}
?>